/*
 * @Author: Zeng Hugh
 * @Date: 2020-09-18 13:32:50
 * @Last Modified by: Zeng Hugh
 * @Last Modified time: 2024-01-11 12:36:57
 */

using RuleJudge.RuleData;
using SLANCommon;
using SLANCommon.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Media;

namespace RuleJudge.Judge
{
    public class MTB_RIF_INH : BaseJudge
    {
        /// <summary>
        /// 构造函数，需要定义项目类型，管，通道信息
        /// </summary>

        #region 定义判读中常量Cutoff,高中低,ΔCt
        // 定义常量：内参阳性判断参考值，结核IS6110阳性判断参考值
        public const double Bacillus_IPC_Positive_Cutoff = 35;  // 枯草芽孢杆菌内参IPC阳性参考值
        public const double MTB_IS6110_Positive_Cutoff = 32;    // 结核分枝杆菌IS6110阳性参考值
        public const double RIF_rpoB_Positive_Cutoff = 35;      // 利福平rpoB阳性参考值
        public const double INH_inhA_Positive_Cutoff = 35;      // 异烟肼inhA阳性参考值
        public const double INH_ahpC_Positive_Cutoff = 35;      // 异烟肼ahpC阳性参考值
        public const double INH_katG_Positive_Cutoff = 35;      // 异烟肼katG阳性参考值

        // 定义常量：结核分枝杆菌浓度高，中，低，极低
        //（1）MTB阳性时，根据rpoB判断高中低；（2）若浓度过低rpoB无扩增，MTB阳性，则判断为极低
        public const double MTB_High_rpoB = 18;
        public const double MTB_Middle_rpoB = 24;
        public const double MTB_Low_rpoB = 31;
        // 定义rpoB极低浓度Ct,INH(inhA、ahpC、katG)极低浓度Ct,部分解决结核浓度非常低时出现大面积“耐药”情况
        public const double RIF_Very_Low = 31;
        public const double INH_Very_Low = 33;

        // 定义常量：各探针各自突变判断参考值RIF(ProbeA...ProbeE), INH(inhA, ahpC, katG)
        public const double rpoB_ProbeA_Mutation = 5;  // ΔCt_ProbeA : rpoB 510,511,512Del，513
        public const double rpoB_ProbeB_Mutation = 5;  // ΔCt_ProbeB : rpoB 515，516
        public const double rpoB_ProbeC_Mutation = 5;  // ΔCt_ProbeC : rpoB 522，517&518Del
        public const double rpoB_ProbeD_Mutation = 5;  // ΔCt_ProbeD : rpoB 526
        public const double rpoB_ProbeE_Mutation = 5;  // ΔCt_ProbeE : rpoB 531, 533
        public const double inhA_ProbeF_Mutation = 5;  // ΔCt_ProbeF : inhA T(-8)A T(-8)C C(-15)T
        public const double ahpC_ProbeG_Mutation = 5;  // ΔCt_ProbeG : ahpC G(-6)A C(-12)T
        public const double katG_ProbeH_Mutation = 5;  // ΔCt_ProbeH : katG 315GGC 315ACC 315AAC
        #endregion

        public MTB_RIF_INH()
        {
            Type = EProjectType.AQ;
            TubesNo = new List<int>() { 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4 };
            ChannelsNo = new List<int>() { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 5 };
        }
        /// <summary>
        /// 判断逻辑
        /// </summary>
        /// <param name="samples">样本列表</param>
        /// <returns>非空：SLAN会弹出内容信息框，进行提示</returns>

        public override string Judge(List<Sample> samples)
        {
            // 在结果判断之前，先检查是否有需要判断的样本
            if (samples.Count == 0) return "无判断样本！";
            // 荧光通道名称，注意：若有荧光通道缺省，Targets[indesx]中下标(index)与名称（值）会有差异！！！
            int FAM = 0;
            int VIC = 1; // int HEX =1;
            int ROX = 2;
            int CY5 = 3;
            int Quasar705 = 4;
            // 定义并初始化自定义变量
            int Negative_Num = 0;
            int Positive_Num = 0;
            bool Negative_Pass = false;
            bool Leak_Negative_Control = false;
            bool Positive_Pass = false;
            bool Leak_Positive_Control = false;

            /* ****************************************************************************************************************
             * 阴性质控(对照)的判读标准：**************************************************************************************
             *（1）第二孔FAM通道Ct大于MTB_IS6110_Positive_Cutoff;**************************************************************
             * (2)第一、二孔CY5通道Ct大于RIF_rpoB_Positive_Cutoff;*************************************************************
             *（3）第一、二孔Quasar705通道Ct小于等于Bacillus_IPC_Positive_Cutoff;**********************************************
             *（4）第三孔VIC通道Ct值小于等于INH_inhA_Positive_Cutoff,CY5通道Ct值小于等于INH_ahpC_Positive_Cutoff;**************
             *（5）第四孔VIC通道Ct值小于等于INH_katG_Positive_Cutoff;**********************************************************
             *（6）第三、四孔Quasar705通道Ct小于等于Bacillus_IPC_Positive_Cutoff.**********************************************
             * ****************************************************************************************************************
             */
            #region 阴性质控(对照)的判读
            List<Sample> NegativeList = samples.Where(s => s.Type == SampleType.Negative).ToList();
            if (NegativeList.Count != 0)  //判断是否存在阴性质控
            {
                foreach (Sample sample in NegativeList)
                {

                    if (// MTB-RIF阴性质控各通道***********************************************
                        // 1. A孔 突变探针A, C, E, rpoB, 内参
                        sample.Tubes[0].Targets[FAM].Result.Ct > RIF_rpoB_Positive_Cutoff &&
                        sample.Tubes[0].Targets[VIC].Result.Ct > RIF_rpoB_Positive_Cutoff &&
                        sample.Tubes[0].Targets[ROX].Result.Ct > RIF_rpoB_Positive_Cutoff &&
                        sample.Tubes[0].Targets[CY5].Result.Ct > RIF_rpoB_Positive_Cutoff &&
                        sample.Tubes[0].Targets[Quasar705].Result.Ct <= Bacillus_IPC_Positive_Cutoff &&
                        // 2. B孔突变探针B, D, rpoB, IS6110, 内参
                        sample.Tubes[1].Targets[FAM].Result.Ct > MTB_IS6110_Positive_Cutoff &&
                        sample.Tubes[1].Targets[VIC].Result.Ct > RIF_rpoB_Positive_Cutoff &&
                        sample.Tubes[1].Targets[ROX].Result.Ct > RIF_rpoB_Positive_Cutoff &&
                        sample.Tubes[1].Targets[CY5].Result.Ct > RIF_rpoB_Positive_Cutoff &&
                        sample.Tubes[1].Targets[Quasar705].Result.Ct <= Bacillus_IPC_Positive_Cutoff &&
                        // MTB-INH阳性质控各通道***********************************************
                        // 1. C孔 ahpC(FAM, CY5); inhA(VIC, ROX); 内参
                        sample.Tubes[2].Targets[FAM].Result.Ct > INH_ahpC_Positive_Cutoff &&  // ahpC_M
                        sample.Tubes[2].Targets[CY5].Result.Ct > INH_ahpC_Positive_Cutoff &&  // inhA
                        sample.Tubes[2].Targets[VIC].Result.Ct > INH_inhA_Positive_Cutoff &&  // inhA_M
                        sample.Tubes[2].Targets[ROX].Result.Ct > INH_inhA_Positive_Cutoff &&  // ahpC
                        sample.Tubes[2].Targets[Quasar705].Result.Ct <= Bacillus_IPC_Positive_Cutoff &&
                        // 2. D孔katG(FAM, VIC); 内参
                        sample.Tubes[3].Targets[FAM].Result.Ct > INH_katG_Positive_Cutoff &&  // katG_M
                        sample.Tubes[3].Targets[VIC].Result.Ct > INH_katG_Positive_Cutoff &&  // katG
                        sample.Tubes[3].Targets[2].Result.Ct <= Bacillus_IPC_Positive_Cutoff
                        )
                    {
                        sample.Conclusion.Add(new Conclusion() { Content = "阴性质控：符合要求", Color=Colors.Green });
                        sample.CustomResults.Add(new CustomResult() { Name = "内参质控", Content = "合格" });
                        sample.CustomResults.Add(new CustomResult() { Name = "中间结论", Content = "/" });
                        Negative_Num ++;
                    }
                    else
                    {
                        sample.Conclusion.Add(new Conclusion() { Content = "阴性质控：不符合要求", Color=Colors.Red });
                        sample.CustomResults.Add(new CustomResult() { Name = "内参质控", Content = "不合格" });
                        sample.CustomResults.Add(new CustomResult() { Name = "中间结论", Content = "/" });
                    }
                }
                if (NegativeList.Count == Negative_Num)
                {
                    Negative_Pass = true;
                }
            }
            // 一次实验中未设置阴性指控则给予提示
            else
            {
                Negative_Pass = true;
                Leak_Negative_Control = true;
            }
            #endregion

            /* *****************************************************************************************************************
             * 阳性质控判读标准：***********************************************************************************************
             *（1）RIF各通道均有扩增曲线 ***************************************************************************************
             *（2）INH各通道均有扩增曲线 ***************************************************************************************
             * *****************************************************************************************************************
             */
            #region 阳性质控(对照)的判读
            List<Sample> PositiveList = samples.Where(s => s.Type == SampleType.HighPositive).ToList();
            if (PositiveList.Count != 0)  //判断是否存在阳性质控
            {
                foreach (Sample sample in PositiveList)
                {
                    #region 判断各通道Ct值是否符合要求
                    if ( // MTB-RIF阳性质控各通道***********************************************
                         // 1. A孔 突变探针A, C, E, rpoB, 内参
                        sample.Tubes[0].Targets[FAM].Result.Ct <= RIF_rpoB_Positive_Cutoff &&
                        sample.Tubes[0].Targets[VIC].Result.Ct <= RIF_rpoB_Positive_Cutoff &&
                        sample.Tubes[0].Targets[ROX].Result.Ct <= RIF_rpoB_Positive_Cutoff &&
                        sample.Tubes[0].Targets[CY5].Result.Ct <= RIF_rpoB_Positive_Cutoff &&
                        sample.Tubes[0].Targets[Quasar705].Result.Ct <= Bacillus_IPC_Positive_Cutoff &&
                        // 2. B孔突变探针B, D, rpoB, IS6110, 内参
                        sample.Tubes[1].Targets[FAM].Result.Ct <= MTB_IS6110_Positive_Cutoff &&
                        sample.Tubes[1].Targets[VIC].Result.Ct <= RIF_rpoB_Positive_Cutoff &&
                        sample.Tubes[1].Targets[ROX].Result.Ct <= RIF_rpoB_Positive_Cutoff &&
                        sample.Tubes[1].Targets[CY5].Result.Ct <= RIF_rpoB_Positive_Cutoff &&
                        sample.Tubes[1].Targets[Quasar705].Result.Ct <= Bacillus_IPC_Positive_Cutoff &&
                        // MTB-INH阳性质控各通道***********************************************
                        // 1. C孔 ahpC(FAM, CY5); inhA(VIC, ROX); 内参
                        sample.Tubes[2].Targets[FAM].Result.Ct <= INH_ahpC_Positive_Cutoff &&  // ahpC_M
                        sample.Tubes[2].Targets[CY5].Result.Ct <= INH_ahpC_Positive_Cutoff &&  // inhA
                        sample.Tubes[2].Targets[VIC].Result.Ct <= INH_inhA_Positive_Cutoff &&  // inhA_M
                        sample.Tubes[2].Targets[ROX].Result.Ct <= INH_inhA_Positive_Cutoff &&  // ahpC
                        sample.Tubes[2].Targets[Quasar705].Result.Ct <= Bacillus_IPC_Positive_Cutoff &&
                        // 2. D孔katG(FAM, VIC); 内参
                        sample.Tubes[3].Targets[FAM].Result.Ct <= INH_katG_Positive_Cutoff &&  // katG_M
                        sample.Tubes[3].Targets[VIC].Result.Ct <= INH_katG_Positive_Cutoff &&  // katG
                        sample.Tubes[3].Targets[2].Result.Ct <= Bacillus_IPC_Positive_Cutoff
                        )
                    {
                        sample.Conclusion.Add(new Conclusion() { Content = "阳性质控：符合要求", Color = Colors.Green });
                        sample.CustomResults.Add(new CustomResult() { Name = "内参质控", Content = "合格" });
                        sample.CustomResults.Add(new CustomResult() { Name = "中间结论", Content = "/" });
                        Positive_Num ++;
                    }
                    else
                    {
                        sample.Conclusion.Add(new Conclusion() { Content = "阳性质控：不符合要求", Color = Colors.Red });
                        sample.CustomResults.Add(new CustomResult() { Name = "内参质控", Content = "不合格" });
                        sample.CustomResults.Add(new CustomResult() { Name = "中间结论", Content = "/" });
                    }
                    #endregion
                }
                if (PositiveList.Count == Positive_Num)
                {
                    Positive_Pass = true;
                }
            }
            //在一次实验中未设置阳性质控，则给与提示
            else
            {
                Positive_Pass = true;
                Leak_Positive_Control = true;
            }
            #endregion

            /* *****************************************************************************************************************
             * 待测（未知）样本UnknownList结果判读标准：************************************************************************
             *（1）阴阳性质控全部合格，按照规定进行结核分枝杆菌和利福平/异烟肼耐药判读；***********************************************
             *（2）否则，直接结束判读，提示阴阳性质控不合格！*******************************************************************
             * *****************************************************************************************************************
             */
            #region 待测（未知）样本结果判读
            List<Sample> UnknownList = samples.Where(s => s.Type == SampleType.Unknown).ToList();   //取出所有“待测样本”
            if (UnknownList.Count != 0)
            {
                foreach (Sample sample in UnknownList)
                {
                    /* ****************************************************************************************************************
                     * 判断待测样本内参是否符合要求：
                     * (1) MTB-RIF两孔内参IPC扩增正常，Ct <= Bacillus_IPC_Positive_Cutoff;
                     * (2) MTB-INH两孔内参IPC扩增正常，Ct <= Bacillus_IPC_Positive_Cutoff;
                     * ****************************************************************************************************************
                    */
                    #region 待测样本内参判读
                    bool Sample_IPC_Positive = false;
                    if (
                        // MTB-RIF内参
                        (sample.Tubes[0].Targets[FAM].Result.Ct <= RIF_rpoB_Positive_Cutoff || sample.Tubes[0].Targets[VIC].Result.Ct <= RIF_rpoB_Positive_Cutoff || sample.Tubes[0].Targets[ROX].Result.Ct <= RIF_rpoB_Positive_Cutoff || sample.Tubes[0].Targets[CY5].Result.Ct <= RIF_rpoB_Positive_Cutoff || sample.Tubes[0].Targets[Quasar705].Result.Ct <= Bacillus_IPC_Positive_Cutoff) &&
                        (sample.Tubes[1].Targets[FAM].Result.Ct <= RIF_rpoB_Positive_Cutoff || sample.Tubes[1].Targets[VIC].Result.Ct <= RIF_rpoB_Positive_Cutoff || sample.Tubes[1].Targets[ROX].Result.Ct <= RIF_rpoB_Positive_Cutoff || sample.Tubes[1].Targets[CY5].Result.Ct <= RIF_rpoB_Positive_Cutoff || sample.Tubes[1].Targets[Quasar705].Result.Ct <= Bacillus_IPC_Positive_Cutoff) &&
                        // MTB-INH内参
                        (sample.Tubes[2].Targets[FAM].Result.Ct <= INH_ahpC_Positive_Cutoff || sample.Tubes[2].Targets[VIC].Result.Ct <= INH_inhA_Positive_Cutoff || sample.Tubes[2].Targets[ROX].Result.Ct <= INH_inhA_Positive_Cutoff || sample.Tubes[2].Targets[CY5].Result.Ct <= INH_ahpC_Positive_Cutoff || sample.Tubes[2].Targets[Quasar705].Result.Ct <= Bacillus_IPC_Positive_Cutoff) &&
                        (sample.Tubes[3].Targets[FAM].Result.Ct <= INH_katG_Positive_Cutoff || sample.Tubes[3].Targets[VIC].Result.Ct <= INH_katG_Positive_Cutoff || sample.Tubes[3].Targets[2].Result.Ct <= Bacillus_IPC_Positive_Cutoff)
                        )
                    {
                        Sample_IPC_Positive = true;
                        sample.CustomResults.Add(new CustomResult() { Name = "内参质控", Content = "合格" });
                    }
                    else // 质控IPC受到抑制，或不符合要求
                    {
                        sample.CustomResults.Add(new CustomResult() { Name = "内参质控", Content = "不合格" });
                        sample.CustomResults.Add(new CustomResult() { Name = "中间结论", Content = "无结果" });
                        sample.Conclusion.Add(new Conclusion() { Content = "结核分枝杆菌检测结果：无结果" + "\n" + "利福平耐药检测结果：无结果" + "\n" + "异烟肼耐药检测结果：无结果", Color=Colors.Yellow });
                    }
                    #endregion

                    /*
                     * *******************************************************************************************************************************
                     * (1) 根据结核分枝杆菌IS6110的Ct值，判断结核分枝杆菌是否检出 ********************************************************************
                     * (2) 获取IS6110和rpoB的Ct值,给出中间结论（高，中，低，极低）********************************************************************
                     * (3) Ct值（IS6110）≤ MTB_Cutoff，报告“结核分枝杆菌检测结果MTB：检出”；否则报告“结核分枝杆菌检测结果MTB：未检出”************
                     * (4) 根据结核MTB检测结果，判断是否需要进一步进行利福平RIF耐药和异烟肼INH耐药结果判读，并分别进行判读 ***************************
                     * (5) 给出最终结果，MTB + RIF + INH (inhA/ahpC/katG) ****************************************************************************
                     * *******************************************************************************************************************************
                     */
                    #region 结核浓度：高、中、低、非常低
                    if (Sample_IPC_Positive)
                    {
                        bool RIF_hasNext = false;
                        bool INH_hasNext = false;
                        // 根据结核分枝杆菌IS6110的Ct值，判断结核分枝杆菌是否检出
                        // 结核分支杆菌检出情况下，根据单拷贝rpoB基因Ct值给出中间结果，即浓度“高”，“中”，“低”，“极低”
                        if (sample.Tubes[1].Targets[FAM].Result.Ct <= MTB_IS6110_Positive_Cutoff)
                        {
                            if (Math.Min(sample.Tubes[0].Targets[CY5].Result.Ct, sample.Tubes[1].Targets[CY5].Result.Ct) <= MTB_High_rpoB)
                            {
                                RIF_hasNext = true;
                                INH_hasNext = true;
                                sample.CustomResults.Add(new CustomResult() { Name = "中间结论", Content = "检出（高）" });
                            }
                            if ((Math.Min(sample.Tubes[0].Targets[CY5].Result.Ct, sample.Tubes[1].Targets[CY5].Result.Ct) <= MTB_Middle_rpoB) &&
                                (Math.Min(sample.Tubes[0].Targets[CY5].Result.Ct, sample.Tubes[1].Targets[CY5].Result.Ct) > MTB_High_rpoB))
                            {
                                RIF_hasNext = true;
                                INH_hasNext = true;
                                sample.CustomResults.Add(new CustomResult() { Name = "中间结论", Content = "检出（中）" });
                            }
                            if ((Math.Min(sample.Tubes[0].Targets[CY5].Result.Ct, sample.Tubes[1].Targets[CY5].Result.Ct) <= MTB_Low_rpoB) &&
                                (Math.Min(sample.Tubes[0].Targets[CY5].Result.Ct, sample.Tubes[1].Targets[CY5].Result.Ct) > MTB_Middle_rpoB))
                            {
                                RIF_hasNext = true;
                                INH_hasNext = true;
                                sample.CustomResults.Add(new CustomResult() { Name = "中间结论", Content = "检出（低）" });
                            }
                            //if (Math.Min(sample.Tubes[0].Targets[CY5].Result.Ct, sample.Tubes[1].Targets[CY5].Result.Ct) > MTB_Low)
                            if (Math.Min(sample.Tubes[0].Targets[CY5].Result.Ct, sample.Tubes[1].Targets[CY5].Result.Ct) > MTB_Low_rpoB)
                            {
                                RIF_hasNext = true;
                                INH_hasNext = true;
                                sample.CustomResults.Add(new CustomResult() { Name = "中间结论", Content = "检出（非常低）" });
                            }
                            sample.Conclusion.Add(new Conclusion() { Content = "结核分枝杆菌检测结果：检出", Color = Colors.Red });
                        }
                        else
                        {
                            RIF_hasNext = false;
                            INH_hasNext = false;
                            sample.CustomResults.Add(new CustomResult() { Name = "中间结论", Content = "未检出" });
                            sample.Conclusion.Add(new Conclusion() { Content = "结核分枝杆菌检测结果：未检出", Color = Colors.Green });
                        }

                        // 计算ΔCt判断是否存在rpoB基因利福平耐药突变,NoCt就是double无穷大，MTB浓度过低时利福平耐药判读结果不可靠！
                        if (RIF_hasNext)
                        {
                            // NoCt就是double无穷大,计算ΔCt时替换成40
                            double Ct_Probe_A = double.IsInfinity(sample.Tubes[0].Targets[FAM].Result.Ct) == true ? 40 : sample.Tubes[0].Targets[FAM].Result.Ct;
                            double Ct_Probe_C = double.IsInfinity(sample.Tubes[0].Targets[VIC].Result.Ct) == true ? 40 : sample.Tubes[0].Targets[VIC].Result.Ct;
                            double Ct_Probe_E = double.IsInfinity(sample.Tubes[0].Targets[ROX].Result.Ct) == true ? 40 : sample.Tubes[0].Targets[ROX].Result.Ct;
                            double Ct_rpoB_1  = double.IsInfinity(sample.Tubes[0].Targets[CY5].Result.Ct) == true ? 40 : sample.Tubes[0].Targets[CY5].Result.Ct;
                            double Ct_Probe_B = double.IsInfinity(sample.Tubes[1].Targets[VIC].Result.Ct) == true ? 40 : sample.Tubes[1].Targets[VIC].Result.Ct;
                            double Ct_Probe_D = double.IsInfinity(sample.Tubes[1].Targets[ROX].Result.Ct) == true ? 40 : sample.Tubes[1].Targets[ROX].Result.Ct;
                            double Ct_rpoB_2  = double.IsInfinity(sample.Tubes[1].Targets[CY5].Result.Ct) == true ? 40 : sample.Tubes[1].Targets[CY5].Result.Ct;
                            // 管A的FAM荧光通道Ct值-管A的CY5荧光通道Ct值
                            double Delta_CtA_rpoB = Ct_Probe_A - Ct_rpoB_1;
                            // 管B的VIC荧光通道Ct值-管B的CY5荧光通道Ct值
                            double Delta_CtB_rpoB = Ct_Probe_B - Ct_rpoB_2;
                            // 管A的VIC荧光通道Ct值-管A的CY5荧光通道Ct值
                            double Delta_CtC_rpoB = Ct_Probe_C - Ct_rpoB_1;
                            // 管B的ROX荧光通道Ct值-管B的CY5荧光通道Ct值
                            double Delta_CtD_rpoB = Ct_Probe_D - Ct_rpoB_2; ;
                            // 管A的ROX荧光通道Ct值-管A的CY5荧光通道Ct值
                            double Delta_CtE_rpoB = Ct_Probe_E - Ct_rpoB_1;

                            // 满足任意一条，“利福平耐药突变检测结果：耐药”；否则报告“利福平耐药突变检测结果：敏感”。
                            if (Delta_CtA_rpoB >= rpoB_ProbeA_Mutation || Delta_CtB_rpoB >= rpoB_ProbeB_Mutation || Delta_CtC_rpoB >= rpoB_ProbeC_Mutation || Delta_CtD_rpoB >= rpoB_ProbeD_Mutation || Delta_CtE_rpoB >= rpoB_ProbeE_Mutation)
                            {
                                // 当rpoB-1、rpoB-2中任意一个Ct值大于31时，若突变探针(A、B、C、D、E)出现至少2条探针ΔCt值 >= 5则认为结核浓度“极低 ”判读为利福平“敏感”
                                if (Math.Max(Ct_rpoB_1,Ct_rpoB_2) > RIF_Very_Low &&
                                    ((Convert.ToInt32(Delta_CtA_rpoB >= rpoB_ProbeA_Mutation) + Convert.ToInt32(Delta_CtB_rpoB >= rpoB_ProbeB_Mutation) + Convert.ToInt32(Delta_CtC_rpoB >= rpoB_ProbeC_Mutation) + Convert.ToInt32(Delta_CtD_rpoB >= rpoB_ProbeD_Mutation) + Convert.ToInt32(Delta_CtE_rpoB >= rpoB_ProbeE_Mutation)) >= 2 ||
                                    (Convert.ToInt32(Ct_Probe_A >= 40) + Convert.ToInt32(Ct_Probe_C >= 40) + Convert.ToInt32(Ct_Probe_E >= 40) >= 2 || Convert.ToInt32(Ct_Probe_B >= 40) + Convert.ToInt32(Ct_Probe_D >= 40) >= 2)))
                                {
                                    sample.Conclusion.Add(new Conclusion() { Content = "\n" + "利福平耐药检测结果：敏感", Color = Colors.Green });
                                }
                                else
                                {
                                    sample.Conclusion.Add(new Conclusion() { Content = "\n" + "利福平耐药检测结果：耐药", Color = Colors.Red });
                                }
                            }
                            else
                            {
                                sample.Conclusion.Add(new Conclusion() { Content = "\n" + "利福平耐药检测结果：敏感", Color = Colors.Green });
                            }
                        }
                        // 满足任意一条，“异烟肼耐药突变检测结果：耐药”；否则报告“异烟肼耐药突变检测结果：敏感”。

                        if (INH_hasNext)
                        {
                            // NoCt就是double无穷大,计算ΔCt时替换成40
                            double Ct_inhA_M = double.IsInfinity(sample.Tubes[2].Targets[ROX].Result.Ct) == true ? 40 : sample.Tubes[2].Targets[ROX].Result.Ct;
                            double Ct_inhA   = double.IsInfinity(sample.Tubes[2].Targets[VIC].Result.Ct) == true ? 40 : sample.Tubes[2].Targets[VIC].Result.Ct;
                            double Ct_ahpC_M = double.IsInfinity(sample.Tubes[2].Targets[FAM].Result.Ct) == true ? 40 : sample.Tubes[2].Targets[FAM].Result.Ct;
                            double Ct_ahpC   = double.IsInfinity(sample.Tubes[2].Targets[CY5].Result.Ct) == true ? 40 : sample.Tubes[2].Targets[CY5].Result.Ct;
                            double Ct_katG_M = double.IsInfinity(sample.Tubes[3].Targets[FAM].Result.Ct) == true ? 40 : sample.Tubes[3].Targets[FAM].Result.Ct;
                            double Ct_katG   = double.IsInfinity(sample.Tubes[3].Targets[VIC].Result.Ct) == true ? 40 : sample.Tubes[3].Targets[VIC].Result.Ct;
                            // 管C的ROX荧光通道Ct值-管C的VIC荧光通道Ct值
                            double Delta_CtF_inhA = Ct_inhA_M - Ct_inhA;
                            // 管C的FAM荧光通道Ct值-管C的CY5荧光通道Ct值
                            double Delta_CtG_ahpC = Ct_ahpC_M - Ct_ahpC;
                            // 管D的FAM荧光通道Ct值-管D的VIC荧光通道Ct值
                            double Delta_CtH_katG = Ct_katG_M - Ct_katG;
                            // 异烟肼inhA,ahpC,katG任意一个基因突变即为异烟肼耐药
                            if (Delta_CtF_inhA >= inhA_ProbeF_Mutation || Delta_CtG_ahpC >= ahpC_ProbeG_Mutation || Delta_CtH_katG >= katG_ProbeH_Mutation)
                            {
                                // 当inhA、ahpC、katG中任意一个Ct值大于33时，若突变探针(inhA_M、ahpC_M、katG_M)出现至少2条探针ΔCt值 >= 5则认为结核浓度“极低 ”判读为异烟肼“敏感”
                                List<double> INH_Ct = new List<double>() { Ct_inhA, Ct_ahpC, Ct_katG };  // Mtah.Max只能比较两个数最大值，此处转换为list类型使用lsit.Max()获取多个数的最大值
                                if (INH_Ct.Max() >= INH_Very_Low &&
                                    (Convert.ToInt32(Delta_CtF_inhA >= inhA_ProbeF_Mutation) + Convert.ToInt32(Delta_CtG_ahpC >= ahpC_ProbeG_Mutation) + Convert.ToInt32(Delta_CtH_katG >= katG_ProbeH_Mutation)) >= 2)
                                {
                                    sample.Conclusion.Add(new Conclusion() { Content = "\n" + "异烟肼耐药检测结果：敏感", Color = Colors.Green });
                                }
                                else
                                {
                                    sample.Conclusion.Add(new Conclusion() { Content = "\n" + "异烟肼耐药检测结果：耐药", Color = Colors.Red });
                                }
                            }
                            else
                            {
                                sample.Conclusion.Add(new Conclusion() { Content = "\n" + "异烟肼耐药检测结果：敏感", Color = Colors.Green });
                            }
                        }
                    }
                    #endregion
                }
            }
            #endregion

            /* *****************************************************************************************************************
             * 重测（未知）样本RetestList结果判读标准：************************************************************************
             *（1）阴阳性质控全部合格，按照规定进行结核分枝杆菌和利福平/异烟肼耐药判读；***********************************************
             *（2）否则，直接结束判读，提示阴阳性质控不合格！*******************************************************************
             * *****************************************************************************************************************
             */
            #region 重测（未知）样本结果判读
            List<Sample> RetestList = samples.Where(s => s.Type == SampleType.RetestSample).ToList();   //取出所有“待测样本”
            if (RetestList.Count != 0)
            {
                foreach (Sample sample in RetestList)
                {
                    /* ****************************************************************************************************************
                     * 判断待测样本内参是否符合要求：
                     * (1) MTB-RIF两孔内参IPC扩增正常，Ct <= Bacillus_IPC_Positive_Cutoff;
                     * (2) MTB-INH两孔内参IPC扩增正常，Ct <= Bacillus_IPC_Positive_Cutoff;
                     * ****************************************************************************************************************
                    */
                    #region 待测样本内参判读
                    bool Sample_IPC_Positive = false;
                    if (
                        // MTB-RIF内参
                        (sample.Tubes[0].Targets[FAM].Result.Ct <= RIF_rpoB_Positive_Cutoff || sample.Tubes[0].Targets[VIC].Result.Ct <= RIF_rpoB_Positive_Cutoff || sample.Tubes[0].Targets[ROX].Result.Ct <= RIF_rpoB_Positive_Cutoff || sample.Tubes[0].Targets[CY5].Result.Ct <= RIF_rpoB_Positive_Cutoff || sample.Tubes[0].Targets[Quasar705].Result.Ct <= Bacillus_IPC_Positive_Cutoff) &&
                        (sample.Tubes[1].Targets[FAM].Result.Ct <= RIF_rpoB_Positive_Cutoff || sample.Tubes[1].Targets[VIC].Result.Ct <= RIF_rpoB_Positive_Cutoff || sample.Tubes[1].Targets[ROX].Result.Ct <= RIF_rpoB_Positive_Cutoff || sample.Tubes[1].Targets[CY5].Result.Ct <= RIF_rpoB_Positive_Cutoff || sample.Tubes[1].Targets[Quasar705].Result.Ct <= Bacillus_IPC_Positive_Cutoff) &&
                        // MTB-INH内参
                        (sample.Tubes[2].Targets[FAM].Result.Ct <= INH_ahpC_Positive_Cutoff || sample.Tubes[2].Targets[VIC].Result.Ct <= INH_inhA_Positive_Cutoff || sample.Tubes[2].Targets[ROX].Result.Ct <= INH_inhA_Positive_Cutoff || sample.Tubes[2].Targets[CY5].Result.Ct <= INH_ahpC_Positive_Cutoff || sample.Tubes[2].Targets[Quasar705].Result.Ct <= Bacillus_IPC_Positive_Cutoff) &&
                        (sample.Tubes[3].Targets[FAM].Result.Ct <= INH_katG_Positive_Cutoff || sample.Tubes[3].Targets[VIC].Result.Ct <= INH_katG_Positive_Cutoff || sample.Tubes[3].Targets[2].Result.Ct <= Bacillus_IPC_Positive_Cutoff)
                        )
                    {
                        Sample_IPC_Positive = true;
                        sample.CustomResults.Add(new CustomResult() { Name = "内参质控", Content = "合格" });
                    }
                    else // 质控IPC受到抑制，或不符合要求
                    {
                        sample.CustomResults.Add(new CustomResult() { Name = "内参质控", Content = "不合格" });
                        sample.CustomResults.Add(new CustomResult() { Name = "中间结论", Content = "无结果" });
                        sample.Conclusion.Add(new Conclusion() { Content = "结核分枝杆菌检测结果：无结果" + "\n" + "利福平耐药检测结果：无结果" + "\n" + "异烟肼耐药检测结果：无结果", Color = Colors.Yellow });
                    }
                    #endregion

                    /*
                     * *******************************************************************************************************************************
                     * (1) 根据结核分枝杆菌IS6110的Ct值，判断结核分枝杆菌是否检出 ********************************************************************
                     * (2) 获取IS6110和rpoB的Ct值,给出中间结论（高，中，低，极低）********************************************************************
                     * (3) Ct值（IS6110）≤ MTB_Cutoff，报告“结核分枝杆菌检测结果MTB：检出”；否则报告“结核分枝杆菌检测结果MTB：未检出”************
                     * (4) 根据结核MTB检测结果，判断是否需要进一步进行利福平RIF耐药和异烟肼INH耐药结果判读，并分别进行判读 ***************************
                     * (5) 给出最终结果，MTB + RIF + INH (inhA/ahpC/katG) ****************************************************************************
                     * *******************************************************************************************************************************
                     */
                    #region 结核浓度：高、中、低、非常低
                    if (Sample_IPC_Positive)
                    {
                        bool RIF_hasNext = false;
                        bool INH_hasNext = false;
                        // 根据结核分枝杆菌IS6110的Ct值，判断结核分枝杆菌是否检出
                        // 结核分支杆菌检出情况下，根据单拷贝rpoB基因Ct值给出中间结果，即浓度“高”，“中”，“低”，“极低”
                        if (sample.Tubes[1].Targets[FAM].Result.Ct <= MTB_IS6110_Positive_Cutoff)
                        {
                            if (Math.Min(sample.Tubes[0].Targets[CY5].Result.Ct, sample.Tubes[1].Targets[CY5].Result.Ct) <= MTB_High_rpoB)
                            {
                                RIF_hasNext = true;
                                INH_hasNext = true;
                                sample.CustomResults.Add(new CustomResult() { Name = "中间结论", Content = "检出（高）" });
                            }
                            if ((Math.Min(sample.Tubes[0].Targets[CY5].Result.Ct, sample.Tubes[1].Targets[CY5].Result.Ct) <= MTB_Middle_rpoB) &&
                               (Math.Min(sample.Tubes[0].Targets[CY5].Result.Ct, sample.Tubes[1].Targets[CY5].Result.Ct) > MTB_High_rpoB))
                            {
                                RIF_hasNext = true;
                                INH_hasNext = true;
                                sample.CustomResults.Add(new CustomResult() { Name = "中间结论", Content = "检出（中）" });
                            }
                            if ((Math.Min(sample.Tubes[0].Targets[CY5].Result.Ct, sample.Tubes[1].Targets[CY5].Result.Ct) <= MTB_Low_rpoB) &&
                               (Math.Min(sample.Tubes[0].Targets[CY5].Result.Ct, sample.Tubes[1].Targets[CY5].Result.Ct) > MTB_Middle_rpoB))
                            {
                                RIF_hasNext = true;
                                INH_hasNext = true;
                                sample.CustomResults.Add(new CustomResult() { Name = "中间结论", Content = "检出（低）" });
                            }
                            //if (Math.Min(sample.Tubes[0].Targets[CY5].Result.Ct, sample.Tubes[1].Targets[CY5].Result.Ct) > MTB_Low)
                            if (Math.Min(sample.Tubes[0].Targets[CY5].Result.Ct, sample.Tubes[1].Targets[CY5].Result.Ct) > MTB_Low_rpoB)
                            {
                                RIF_hasNext = true;
                                INH_hasNext = true;
                                sample.CustomResults.Add(new CustomResult() { Name = "中间结论", Content = "检出（非常低）" });
                            }
                            sample.Conclusion.Add(new Conclusion() { Content = "结核分枝杆菌检测结果：检出", Color = Colors.Red });
                        }
                        else
                        {
                            RIF_hasNext = false;
                            INH_hasNext = false;
                            sample.CustomResults.Add(new CustomResult() { Name = "中间结论", Content = "未检出" });
                            sample.Conclusion.Add(new Conclusion() { Content = "结核分枝杆菌检测结果：未检出", Color = Colors.Green });
                        }

                        // 计算ΔCt判断是否存在rpoB基因利福平耐药突变,NoCt就是double无穷大，MTB浓度过低时利福平耐药判读结果不可靠！
                        if (RIF_hasNext)
                        {
                            // NoCt就是double无穷大,计算ΔCt时替换成40
                            double Ct_Probe_A = double.IsInfinity(sample.Tubes[0].Targets[FAM].Result.Ct) == true ? 40 : sample.Tubes[0].Targets[FAM].Result.Ct;
                            double Ct_Probe_C = double.IsInfinity(sample.Tubes[0].Targets[VIC].Result.Ct) == true ? 40 : sample.Tubes[0].Targets[VIC].Result.Ct;
                            double Ct_Probe_E = double.IsInfinity(sample.Tubes[0].Targets[ROX].Result.Ct) == true ? 40 : sample.Tubes[0].Targets[ROX].Result.Ct;
                            double Ct_rpoB_1 = double.IsInfinity(sample.Tubes[0].Targets[CY5].Result.Ct) == true ? 40 : sample.Tubes[0].Targets[CY5].Result.Ct;
                            double Ct_Probe_B = double.IsInfinity(sample.Tubes[1].Targets[VIC].Result.Ct) == true ? 40 : sample.Tubes[1].Targets[VIC].Result.Ct;
                            double Ct_Probe_D = double.IsInfinity(sample.Tubes[1].Targets[ROX].Result.Ct) == true ? 40 : sample.Tubes[1].Targets[ROX].Result.Ct;
                            double Ct_rpoB_2 = double.IsInfinity(sample.Tubes[1].Targets[CY5].Result.Ct) == true ? 40 : sample.Tubes[1].Targets[CY5].Result.Ct;
                            // 管A的FAM荧光通道Ct值-管A的CY5荧光通道Ct值
                            double Delta_CtA_rpoB = Ct_Probe_A - Ct_rpoB_1;
                            // 管B的VIC荧光通道Ct值-管B的CY5荧光通道Ct值
                            double Delta_CtB_rpoB = Ct_Probe_B - Ct_rpoB_2;
                            // 管A的VIC荧光通道Ct值-管A的CY5荧光通道Ct值
                            double Delta_CtC_rpoB = Ct_Probe_C - Ct_rpoB_1;
                            // 管B的ROX荧光通道Ct值-管B的CY5荧光通道Ct值
                            double Delta_CtD_rpoB = Ct_Probe_D - Ct_rpoB_2; ;
                            // 管A的ROX荧光通道Ct值-管A的CY5荧光通道Ct值
                            double Delta_CtE_rpoB = Ct_Probe_E - Ct_rpoB_1;

                            // 满足任意一条，“利福平耐药突变检测结果：耐药”；否则报告“利福平耐药突变检测结果：敏感”。
                            // 满足任意一条，“利福平耐药突变检测结果：耐药”；否则报告“利福平耐药突变检测结果：敏感”。
                            if (Delta_CtA_rpoB >= rpoB_ProbeA_Mutation || Delta_CtB_rpoB >= rpoB_ProbeB_Mutation || Delta_CtC_rpoB >= rpoB_ProbeC_Mutation || Delta_CtD_rpoB >= rpoB_ProbeD_Mutation || Delta_CtE_rpoB >= rpoB_ProbeE_Mutation)
                            {
                                // 当rpoB-1、rpoB-2中任意一个Ct值大于31时，若突变探针(A、B、C、D、E)出现至少2条探针ΔCt值 >= 5则认为结核浓度“极低 ”判读为利福平“敏感”
                                if (Math.Max(Ct_rpoB_1, Ct_rpoB_2) > RIF_Very_Low &&
                                    ((Convert.ToInt32(Delta_CtA_rpoB >= rpoB_ProbeA_Mutation) + Convert.ToInt32(Delta_CtB_rpoB >= rpoB_ProbeB_Mutation) + Convert.ToInt32(Delta_CtC_rpoB >= rpoB_ProbeC_Mutation) + Convert.ToInt32(Delta_CtD_rpoB >= rpoB_ProbeD_Mutation) + Convert.ToInt32(Delta_CtE_rpoB >= rpoB_ProbeE_Mutation)) >= 2 ||
                                    (Convert.ToInt32(Ct_Probe_A >= 40) + Convert.ToInt32(Ct_Probe_C >= 40) + Convert.ToInt32(Ct_Probe_E >= 40) >= 2 || Convert.ToInt32(Ct_Probe_B >= 40) + Convert.ToInt32(Ct_Probe_D >= 40) >= 2)))
                                {
                                    sample.Conclusion.Add(new Conclusion() { Content = "\n" + "利福平耐药检测结果：敏感", Color = Colors.Green });
                                }
                                else
                                {
                                    sample.Conclusion.Add(new Conclusion() { Content = "\n" + "利福平耐药检测结果：耐药", Color = Colors.Red });
                                }
                            }
                            else
                            {
                                sample.Conclusion.Add(new Conclusion() { Content = "\n" + "利福平耐药检测结果：敏感", Color = Colors.Green });
                            }
                        }
                        // 满足任意一条，“异烟肼耐药突变检测结果：耐药”；否则报告“异烟肼耐药突变检测结果：敏感”。

                        if (INH_hasNext)
                        {
                            // NoCt就是double无穷大,计算ΔCt时替换成40
                            double Ct_inhA_M = double.IsInfinity(sample.Tubes[2].Targets[ROX].Result.Ct) == true ? 40 : sample.Tubes[2].Targets[ROX].Result.Ct;
                            double Ct_inhA = double.IsInfinity(sample.Tubes[2].Targets[VIC].Result.Ct) == true ? 40 : sample.Tubes[2].Targets[VIC].Result.Ct;
                            double Ct_ahpC_M = double.IsInfinity(sample.Tubes[2].Targets[FAM].Result.Ct) == true ? 40 : sample.Tubes[2].Targets[FAM].Result.Ct;
                            double Ct_ahpC = double.IsInfinity(sample.Tubes[2].Targets[CY5].Result.Ct) == true ? 40 : sample.Tubes[2].Targets[CY5].Result.Ct;
                            double Ct_katG_M = double.IsInfinity(sample.Tubes[3].Targets[FAM].Result.Ct) == true ? 40 : sample.Tubes[3].Targets[FAM].Result.Ct;
                            double Ct_katG = double.IsInfinity(sample.Tubes[3].Targets[VIC].Result.Ct) == true ? 40 : sample.Tubes[3].Targets[VIC].Result.Ct;
                            // 管C的ROX荧光通道Ct值-管C的VIC荧光通道Ct值
                            double Delta_CtF_inhA = Ct_inhA_M - Ct_inhA;
                            // 管C的FAM荧光通道Ct值-管C的CY5荧光通道Ct值
                            double Delta_CtG_ahpC = Ct_ahpC_M - Ct_ahpC;
                            // 管D的FAM荧光通道Ct值-管D的VIC荧光通道Ct值
                            double Delta_CtH_katG = Ct_katG_M - Ct_katG;
                            // 异烟肼inhA,ahpC,katG任意一个基因突变即为异烟肼耐药
                            if (Delta_CtF_inhA >= inhA_ProbeF_Mutation || Delta_CtG_ahpC >= ahpC_ProbeG_Mutation || Delta_CtH_katG >= katG_ProbeH_Mutation)
                            {
                                // 当inhA、ahpC、katG中任意一个Ct值大于33时，若突变探针(inhA_M、ahpC_M、katG_M)出现至少2条探针ΔCt值 >= 5则认为结核浓度“极低 ”判读为异烟肼“敏感”
                                List<double> INH_Ct = new List<double>() { Ct_inhA, Ct_ahpC, Ct_katG };  // Mtah.Max只能比较两个数最大值，此处转换为list类型使用lsit.Max()获取多个数的最大值
                                if (INH_Ct.Max() >= INH_Very_Low &&
                                    (Convert.ToInt32(Delta_CtF_inhA >= inhA_ProbeF_Mutation) + Convert.ToInt32(Delta_CtF_inhA >= inhA_ProbeF_Mutation) + Convert.ToInt32(Delta_CtF_inhA >= inhA_ProbeF_Mutation)) >= 2)
                                {
                                    sample.Conclusion.Add(new Conclusion() { Content = "\n" + "异烟肼耐药检测结果：敏感", Color = Colors.Green });
                                }
                                else
                                {
                                    sample.Conclusion.Add(new Conclusion() { Content = "\n" + "异烟肼耐药检测结果：敏感", Color = Colors.Green });
                                }
                            }
                            else
                            {
                                sample.Conclusion.Add(new Conclusion() { Content = "\n" + "异烟肼耐药检测结果：敏感", Color = Colors.Green });
                            }
                        }
                    }
                    #endregion
                }
            }
            #endregion

            /* ****************************************************************************************************************
             * 实验中未设置阴阳性质控不影响判读，仅给出弹窗提醒缺少质控 ************************************************************
             * 阴阳性质控不合格的，则给出提醒，但仍然进行判读 *********************************************************************
             * ****************************************************************************************************************
             */
            #region 质控结果异常（不合格，缺失）提示
            if (!Negative_Pass || !Positive_Pass)
            {
                MessageBox.Show("一个或多个阴阳性质控不符合要求！是否忽略？", "质控提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            if (Leak_Negative_Control || Leak_Positive_Control)
            {
                MessageBox.Show("实验缺少阴性质控或阳性质控！是否忽略？", "质控提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            #endregion

            return string.Empty;
        }
    }
}